package com.example.administrator.startproject_llh110.model;


import com.example.administrator.startproject_llh110.bean.WeatherResult;
import com.example.administrator.startproject_llh110.iface.WeatherIface;
import com.example.administrator.startproject_llh110.iface.WeatherListener;
import com.example.administrator.startproject_llh110.service.Weather51Service;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/3/14.
 */

public class Weather51Model implements WeatherIface{
    private Weather51Service service;
    private Retrofit retrofit;
    String BASE_URL = "http://weather.51wnl.com/weatherinfo/";

    public Weather51Model() {
        retrofit=new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getWeather(String cityno, final WeatherListener listener) {

        service=retrofit.create(Weather51Service.class);
        Call<WeatherResult> call = service.getResult(cityno,1);

        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    listener.onResponse(response.body().getWeatherInfo());
                else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });
    }
}
